﻿using SWBetongXaydung.DAL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SWBetongXaydung.BLL
{
    class HeThong
    {
        internal static DAL.NguoiSuDung GetNguoiSuDung(string tenDangNhap, string matKhau)
        {
            if (tenDangNhap == null || matKhau == null)
                return null;
            tenDangNhap = tenDangNhap.ToLower();
            //if (tenDangNhap == "admin" && matKhau == "admin")
            //{
            //    return new DAL.NguoiSuDung()
            //    {
            //        HoTen = "Nguyễn Văn",
            //        TenDangNhap = tenDangNhap,
            //        MatKhau = matKhau,
            //        VaiTro = NguoiSuDung.VaiTro_QuanTriNguoiDung
            //    };
            //}
            //else if (tenDangNhap == "user" && matKhau == "123")
            //{
            //    return new NguoiSuDung()
            //    {
            //        HoTen = "Nguyễn",
            //        TenDangNhap = tenDangNhap,
            //        MatKhau = matKhau,
            //        VaiTro = NguoiSuDung.VaiTro_TacNghiep
            //    };
            //}
            //else
            //    return null;
            return DBContextHelper.GetContext().NguoiSuDungDbSet.Where(e => e.TenDangNhap == tenDangNhap && e.MatKhau == matKhau).SingleOrDefault();
        }

        internal static void DoiMatKhau(NguoiSuDung m_NguoiSuDung, string matKhauMoi)
        {
            using (var context = DBContextHelper.GetContext())
            {
                NguoiSuDung nguoiSuDung = context.NguoiSuDungDbSet.Where(
                    nsd => nsd.TenDangNhap == m_NguoiSuDung.TenDangNhap &&
                        nsd.MatKhau == m_NguoiSuDung.MatKhau).FirstOrDefault();
                if (nguoiSuDung == null)
                    throw new Exception("Thông tin người sử dụng không đồng nhất!");
                else
                {
                    try
                    {
                        nguoiSuDung.MatKhau = matKhauMoi;
                        context.SaveChanges();
                    }
                    catch
                    {
                        throw new Exception("Không thể thay đổi mật khẩu được!");
                    }
                }
            }
        }

        internal static bool AddNew(NguoiSuDung user)
        {
            try
            {
                using (var context = DBContextHelper.GetContext())
                {
                    context.NguoiSuDungDbSet.Add(user);
                    context.SaveChanges();
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

        }

        internal static List<NguoiSuDung> GetListNguoiSuDung()
        {
            using (var context = DBContextHelper.GetContext())
            {
                var ds = from e in context.NguoiSuDungDbSet
                         select e;
                return ds.ToList();
            }
        }

        internal static bool Delete_NguoiSuDung(NguoiSuDung SelectedNguoiSuDung)
        {
            using (var context = DBContextHelper.GetContext())
            {
                try
                {
                    var nsd = (from e in context.NguoiSuDungDbSet
                               where e.TenDangNhap == SelectedNguoiSuDung.TenDangNhap
                               select e).SingleOrDefault();
                    if (nsd != null)
                        context.NguoiSuDungDbSet.Remove(nsd);
                    context.SaveChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        internal static bool Update_NguoiSuDung(NguoiSuDung nguoiSuDung)
        {
            using (var context = DBContextHelper.GetContext())
            {
                NguoiSuDung nsdung = context.NguoiSuDungDbSet.Where(
                    nsd => nsd.TenDangNhap == nguoiSuDung.TenDangNhap).FirstOrDefault();
                if (nguoiSuDung == null)
                    return false;
                else
                {
                    try
                    {
                        nsdung.HoTen = nguoiSuDung.HoTen;
                        context.SaveChanges();
                        return true;
                    }
                    catch
                    {
                        return false;
                    }
                }
            }
        }
    }
}
